﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace yaTDL.Controls
{
    public class NumericTextBox:TextBox 
    {
        /// <summary>
        /// Initializes a new instace of Num
        /// </summary>
        public NumericTextBox()
        {
            DataObject.AddPastingHandler(this, new DataObjectPastingEventHandler(handlePaste));
        }



        /// <summary>
        /// Makes it impossible to paste anything but numeric characters into the TextBox.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void handlePaste(object sender, DataObjectPastingEventArgs e)
        {
            if (!AreAllValidNumericChars(e.DataObject.GetData(DataFormats.Text) as String))
            { e.CancelCommand(); }
        }

        /// <summary>
        /// Makes it impossible to enter anything but numeric characters into the TextBox.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            e.Handled = !AreAllValidNumericChars(e.Text);
            base.OnPreviewTextInput(e);            
        }
        
        /// <summary>
        /// Makes it impossible to enter a space into the TextBox.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            e.Handled = e.Key == System.Windows.Input.Key.Space ? true : false;
            base.OnPreviewKeyDown(e);
        }

        /// <summary>
        /// Makes it impossible to drop anything but a string with only numeric chars into the TextBox.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDrop(System.Windows.DragEventArgs e)
        {
            e.Handled = !AreAllValidNumericChars(e.Data.GetData(DataFormats.Text) as String);
            base.OnDrop(e);
        }

        /// <summary>
        /// Indicates that the content cannot be dropped unless it is a string with only numeric chars.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDragOver(System.Windows.DragEventArgs e)
        {
            if (!AreAllValidNumericChars(e.Data.GetData(DataFormats.Text) as String))
            {
                e.Handled = true;
                e.Effects = DragDropEffects.None;
            }
            base.OnDragOver(e);
        }

        /// <summary>
        /// Checks if all characters in the string are numeric chars.
        /// </summary>
        /// <param name="str">The string to check.</param>
        /// <returns>Returns true if all characters are numeric chars.</returns>
        bool AreAllValidNumericChars(string str)
        {
            if (!String.IsNullOrEmpty(str))
            {
                bool ret = true;
                if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
                    str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
                    return ret;

                int l = str.Length;
                for (int i = 0; i < l; i++)
                {
                    char ch = str[i];
                    ret &= Char.IsDigit(ch);
                }

                return ret;
            }

            return true;
        }
    
    }


}
